package com.jkwzms.base.price.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jkwzms.base.price.entity.PriceModel;
import com.jkwzms.base.price.service.PriceService;
import com.jkwzms.base.xmb.entity.XmbModel;
import com.jkwzms.common.Constants;
import com.jkwzms.common.entity.search.SearchRequest;
import com.jkwzms.common.entity.search.Searchable;
import com.jkwzms.common.inject.annotation.BaseComponent;
import com.jkwzms.common.pagination.Pagination;
import com.jkwzms.common.web.Servlets;
import com.jkwzms.common.web.controller.BaseCRUDController;


@Controller
@RequestMapping(value ="/base/price")
public class PriceController extends BaseCRUDController<PriceModel, Long>{
    
    @Autowired
    @BaseComponent
    private PriceService  priceService;
    
    /**
     * 页面跳转
     * @return
     */
    @RequestMapping(value = "view")
    public String view(){
        return "base/price/list";
    }
    
    /**
     * 数据源展示列表
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public Pagination<PriceModel> list(@RequestParam("pageNumber") int pageNumber,@RequestParam("pageSize") int pageSize, HttpServletRequest request){
        Pagination<PriceModel> list = null;
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, Constants.SEARCH_PREFIX);
        Searchable searchable = SearchRequest.newSearchable();
        searchable.addSearchParams(searchParams);
        searchable.setPage(pageNumber - 1, pageSize);
        list = priceService.findAll(searchable);
        return list;
    }
    
    /**
     * 批量删除部门信息
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "deleteByIds")
    public Map<String, Object> delete(@RequestParam (value = "ids")String ids,HttpServletRequest request ,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String[] id = ids.split(",");
            for(int i =0;i<id.length; i++){
                priceService.delete(Long.parseLong(id[i]));
            }
            map.put("result", true);
            map.put("msg", "删除成功！");
            return map;
        } catch (Exception e) {
            // TODO: handle exception
            map.put("result", false);
            map.put("message", "删除失败！");
            return map;
        }
    }
    
    /**
     * 添加部门信息
     * @param xmb
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "addMessage")
    public Map<String, Object> add(PriceModel price,HttpServletRequest request ,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            priceService.save(price);
            map.put("result", true);
            map.put("msg", "添加成功！");
            return map;
        } catch (Exception e) {
            // TODO: handle exception
            map.put("result", false);
            map.put("message", "添加失败！");
            return map;
        }
        
    }
    
    /**
     * 修改部门信息
     * @param xmb
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "editMessage")
    public Map<String, Object> edit(PriceModel price,HttpServletRequest request ,HttpServletResponse response){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            priceService.update(price);
            map.put("result", true);
            map.put("msg", "修改成功！");
            return map;
        } catch (Exception e) {
            // TODO: handle exception
            map.put("result", false);
            map.put("message", "修改失败！");
            return map;
        }
        
    }
    
    /**
     * 根据中心类别获取相应的数据
     * @param pid
     * @return
     */
    public List<PriceModel> getByPid(Long pid){
        List<PriceModel>  list = priceService.getByPid(pid);
        return list;
    }
}
